package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/v1/file")
public class UploadController {
    @PostMapping("/upload")
    public JsonResult upload(MultipartFile file) throws IOException {
        String filename= file.getOriginalFilename();
        String suf=filename.substring(filename.lastIndexOf("."));
        filename=UUID.randomUUID()+suf;
        log.debug(filename);

        String staticFile="E:/files/pictures";
        SimpleDateFormat format = new SimpleDateFormat("/yyyy/MM/dd/");
        String path= format.format(new Date());
        File dir=new File(staticFile+path);
        if(!dir.exists())
        {
            dir.mkdirs();
        }
        String filepath=staticFile+format+filename;
        file.transferTo(new File(filepath));
        return JsonResult.ok(path+filename);
    }
}
